package com.gaswell.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaswell.entity.Qba01;
import com.gaswell.mapper.Qba01Mapper;
import com.gaswell.pojo.ReciveCycleData;
import com.gaswell.service.IQba01MService;
import com.gaswell.service.IQba01Service;
import com.gaswell.service.IQba01Service2;
import com.gaswell.service.QjService;
import com.gaswell.utils.DateUtils;
import com.gaswell.utils.ObjectUtils;
import com.gaswell.utils.StatusDetect;
import com.gaswell.vo.*;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;


@Service
@DS("oracle")
public class Qba01Service2Impl extends ServiceImpl<Qba01Mapper, Qba01> implements IQba01Service2 {

    @Autowired
    private Qba01Mapper qba01Mapper;
    @Autowired
    private QjService qjService;
    @Autowired
    IQba01Service qba01Service;


    @Override
    @Async("threadPoolTaskExecutor")
    public Future<Boolean> pageForMultiThread(LambdaQueryWrapper<Qba01> qba01LambdaQueryWrapper, int now, int size, Map map) {
        Page<Qba01> page = new Page<>(now, size);
        LambdaQueryWrapper<Qba01> qba01LambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        Page<Qba01> page1 = this.page(page, qba01LambdaQueryWrapper1);
        for (Qba01 record : page1.getRecords()) {
            if (!map.containsKey(record.getJh())) {
                map.put(record.getJh(), 0);
            }
            map.put(record.getJh(), map.get(record.getJh()));
        }


        return new AsyncResult<>(true);
    }

    @Override
    @Async("threadPoolTaskExecutor_forqba01")
    public Future<List> pageForMultiThreadForExcel(LambdaQueryWrapper<Qba01> qba01LambdaQueryWrapper, int now, int size) {
        Page<Qba01> rtuDataPage = new Page<Qba01>(now + 1, size);
        Page<Qba01> page = this.page(rtuDataPage);
        List<Qba01> records = page.getRecords();
        return new AsyncResult<>(records);
    }

    @Override
    @Async("threadPoolTaskExecutor_forqba01count")
    public CompletableFuture<String> countQba01ForMultiThread(List<String> ywbhs, List<String> zwbhs, int current, int size, Date start, Map<String, Qba01Count> result) {
//      拿到分页之后的日数据以及各种参数
        IPage byPropertiesDirectSingleThread = qba01Service.getByPropertiesDirectSingleThread(new Timestamp(start.getTime()), null, null, null, 0, current, size
                , 1, null, ywbhs, zwbhs);
//      判断有没有问题，有的话存入map中
        List records = byPropertiesDirectSingleThread.getRecords();
        for (int i = 0; i < records.size(); i++) {
            double g = 0.6;
            double d = 62;
            double vg = 2.5;
            Qba01Vo2 qba01 = (Qba01Vo2) records.get(i);
            if (qba01.getD() != null)
                d = qba01.getD().doubleValue();
            if (qba01.getG() != null)
                g = qba01.getG().doubleValue();
            if (qba01.getVg() != null)
                vg = qba01.getVg().doubleValue();

            ReciveCycleData reciveCycleData = new ReciveCycleData()
                    .setJkwd(qba01.getJkwd() == null ? 0 : qba01.getJkwd().doubleValue())
                    .setTgyl(qba01.getPjty() == null ? 0 : qba01.getPjty().doubleValue())
                    .setYgyl(qba01.getPjyy() == null ? 0 : qba01.getPjyy().doubleValue())
                    .setWsyl(qba01.getWsyl() == null ? 0 : qba01.getWsyl().doubleValue())
                    .setCqill(qba01.getRcql() == null ? 0 : qba01.getRcql().doubleValue());
            String jiYe = StatusDetect.isJiYe(reciveCycleData, d, g, vg);
            String shuiHeWu = StatusDetect.isShuiHeWu(reciveCycleData, g);
            qba01.setJy(jiYe)
                    .setDd(shuiHeWu);
            //      0 不积液 1 积液
            //      2 不冻堵 1 冻堵
            // 代表出现了问题，如果没问题的话不需要插入新数据
            if (jiYe.equals("1") || shuiHeWu.equals("1")) {
                if (!result.containsKey(qba01.getJh())) {
                    result.put(qba01.getJh(), new Qba01Count().setJh(qba01.getJh()));
                }
                Qba01Count tmpQba01Count = result.get(qba01.getJh());
                tmpQba01Count.getDates().add(qba01.getRq().toString());
                StringBuilder condition = new StringBuilder();
                if (jiYe.equals("1"))
                    condition.append("积液,");
                if (shuiHeWu.equals("1"))
                    condition.append("冻堵,");
                tmpQba01Count.getConditions().add(condition.toString());
                result.put(qba01.getJh(), tmpQba01Count);
            }
        }
        return CompletableFuture.completedFuture("yes");
    }
}
